%% Multiple Plots on Same Figure

%Make two synthetic data sets
X=linspace(0,10,100)
Y1=sin(X*5)
Y2=sin(X*5).^2

%create a figure a plot
figure
plot(X,Y1,'g')
xlabel('Time (s)')
ylabel('Voltage')

hold on % With this command, all plots will be added to the same figure.
plot(X,Y2,'r')
legend('Exp 1','Exp 2') %Give to legend entries


%% Ballistic Trajectory

g=9.8 % (m/s/s)
LaunchAngleDeg = 45 % angle in degrees
LaunchAngle = LaunchAngleDeg*pi/180
Speed0 = 10 % initial speed (m/s)

%Initial conditions
X0=0;
Y0=.01;

Vx0=Speed0*cos(LaunchAngle);
Vy0=Speed0*sin(LaunchAngle);

%Constant acceleration
aX = 0;
aY = -g;

% Time point to evaluate
TimeArray = linspace(0,10,100)

%This is an anonymous function we will use for both X and Y directions. 
x_t = @(x0,v0,a,t)x0 + v0*t + 1/2*a*t.^2

%Note, the variable names don't matter. They could be anything such as:
%x_t = @(a,b,c,d)a + b*d + 1/2*c*d.^2
%only the order of inputs matter. 

%Calculate the position at each time point in the TimeArray
XvsT = x_t(X0,Vx0,aX,TimeArray)
YvsT = x_t(Y0,Vy0,aY,TimeArray)

%Plot the result. 
figure
plot(XvsT,YvsT)
xlabel('X (m)')
ylabel('Y (m)')

%Our object went below ground! 


%% Ballistic Trajectory 2
% This time we will build the array one element at a time and stop when we
% hit the ground

clear %clear all variables (also clears our anonymous function)

%Setup the problem again

g=9.8 % (m/s/s)
LaunchAngleDeg = 45 % angle in degrees
LaunchAngle = LaunchAngleDeg*pi/180
Speed0 = 10 % initial speed (m/s)

X0=0;
Y0=.01;

Vx0=Speed0*cos(LaunchAngle);
Vy0=Speed0*sin(LaunchAngle);

aX = 0;
aY = -g;

x_t = @(x0,v0,a,t)x0 + v0*t + 1/2*a*t.^2

%Setup and intialize variable for our 'while' loop.
CurrentY = Y0;
TimeStep = .01 % (s)
Cnt=1;
CurrentTime = 0;
XvsT = []; %Start with empty array
YvsT = []; %Start with empty array
while CurrentY >0 %Stop if we hit the ground
    Time = (Cnt-1)*TimeStep; %Time point we are evaluating in this iteration
    CurrentX=x_t(X0,Vx0,aX,Time); %Use our function to calc position
    CurrentY=x_t(Y0,Vy0,aY,Time); %Use our function to calc position
    XvsT(Cnt)=CurrentX; %Write position in next element of array
    YvsT(Cnt)=CurrentY;  %Write position in next element of array
    Cnt = Cnt +1; %Increment our counter.
end


figure
plot(XvsT,YvsT)
xlabel('X (m)')
ylabel('Y (m)')
axis equal % This makes X,Y axis have the same scale


%% Animate our trajectory


T = size(XvsT,2); %Number of array elements 


figure %Create figure
Ax = axes() %Create axes and store as variable for later use. 

for tt=1:T  %loop over each time point
    plot(XvsT(1:tt),YvsT(1:tt)) %plot trajectory up to element 'tt'
    %Note the indexing:  This means return the elements from 1 to tt
    hold on %next plot will write on top of existing (keeping existing)
    plot(XvsT(tt),YvsT(tt),'ro','linewidth',5) %Plot the objects current position
    Ax.XLim = [min(XvsT) max(XvsT)]; %Set the axis limits to the limits of total trajctory
    Ax.YLim = [min(YvsT) max(YvsT)];
    xlabel('X (m)');
    ylabel('Y (m)');
    pause(TimeStep);  %Pause for 'TimeStep' seconds. 
    hold off %Next plot will make erase old plots
end

%This should be close to real time, but a bit slower since there is computational time
% in addition to our 'pause' statement. Throw something up 2.5 m into the air
%and compare.  






